package ustc.xiaoyang.karl.simulation;

import java.util.Arrays;

public class SpiralMatrix2_59 {
    public static void main(String[] args) {
        System.out.println(Arrays.deepToString(new SpiralMatrix2_59().generateMatrix(6)));
    }
    public int[][] generateMatrix(int n){
        int startX = 0;
        int startY = 0;
        int offset = 1;
        int count = 1;
        int loop = n/2;
        int[][] ans = new int[n][n];
        while(loop > 0){
            int j = startY;
            int i = startX;
            for (; j < n - offset; j++) {
                ans[i][j] = count++;
            }
            for (; i < n - offset; i++) {
                ans[i][j] = count++;
            }
            for (; j > startY; j--) {
                ans[i][j] = count++;
            }
            for (; i > startX; i--){
                ans[i][j] = count++;
            }
            loop /= 2;
            startY++;
            startX++;
            offset++;
        }
        if (n % 2 == 1) ans[n / 2][n / 2] = count;
        return ans;
    }
}
